<?php

namespace app\api\controller\v2;

use app\api\controller\Common;
use think\Db;
use think\Request;

class Taskuniversal extends Common {
    /**
     * @var mixed
     */
    public $params = null;
    /**
     * @param Request $request
     */
    public function __construct(Request $request = null) {
        parent::__construct($request);
        $this->params = getRsaPostParamArr2();
    }

    public function enlists()
    {
        $page     = $this->params['page'] ?? 1;
        $pagesize = $this->params['pagesize'] ?? 20;
        $pagesize = $pagesize > 20 ? 20 : $pagesize;
        $joined = Db::name('task_universal_records')->master()->where(['user_id'=>$this->member['id']])->column('task_universal_id');
        $data = Db::name('task_universal')
                    ->where(['delete_time'=>0, 'id'=>['not in', $joined],'channel'=>1])
                    ->order('sort desc')
                    ->field('id,name,image,value')
                    ->page($page)
                    ->limit($pagesize)
                    ->select();
        return $this->pass($data);
    }
    public function info($returnArray = false)
    {
        $id = $this->params['id'] ?? 0;
        $data = Db::name('task_universal')->where(['id'=>$id,'delete_time'=>0,'channel'=>1])->field('id,name,image,description,value')->find();
        if (!$data) exit(json_encode(['code'=>400, 'msg'=>'任务找不到了~']));
        $data['receive_info'] = Db::name('task_universal_records')
                                    ->master()
                                    ->where(['user_id'=>$this->member['id'], 'task_universal_id'=>$id,'channel'=>1])
                                    ->field('delete_time',true)
                                    ->find();

        if ($data['receive_info']) {
            if ($data['receive_info']['create_time']) $data['receive_info']['create_time'] = date('Y-m-d H:i', $data['receive_info']['create_time']);
            if ($data['receive_info']['update_time']) $data['receive_info']['update_time'] = date('Y-m-d H:i', $data['receive_info']['update_time']);
            if ($data['receive_info']['audit_time']) $data['receive_info']['audit_time'] = date('Y-m-d H:i', $data['receive_info']['audit_time']);
            $data['receive_info']['images'] = $data['receive_info']['images'] ? explode(',', $data['receive_info']['images']) : [];
        }
        return $returnArray ? $data : $this->pass($data);
    }

    public function receive()
    {
        $data = $this->info(true);
        if ($data['receive_info']) return $this->miss('该任务已接取');
        $res = Db::name('task_universal_records')->insert([
            'user_id'=>$this->member['id'],
            'task_universal_id'=>$data['id'],
            'state'=>0,
            'value'=>$data['value'],
            'create_time'=>time(),
            'channel'=>1
        ]);
        if (!$res) return $this->miss('接取失败,请稍后再试');
        return $this->pass('接取成功,快去完成任务并提交吧');
    }

    public function submit()
    {
        $id     = $this->params['id'] ?? 0;
        $images     = $this->params['images'] ?? '';
        $content     = $this->params['content'] ?? '';

        $receive_info = Db::name('task_universal_records')
                            ->master()
                            ->where(['user_id'=>$this->member['id'], 'task_universal_id'=>$id,'channel'=>1])
                            ->find();

        if (!$receive_info) return $this->miss('提交失败,未接取的任务');
        if ($receive_info['state'] >= 1) return $this->miss('已提交过,请勿重复提交');
        if ($receive_info['state'] == -2) return $this->miss('无权提交');
        
        $res = Db::name('task_universal_records')->where(['id'=>$receive_info['id']])->update([
            'images'=>$images,
            'content'=>$content,
            'state'=>1,
            'update_time'=>time()
        ]);
        if (!$res) return $this->miss('提交失败,请稍后再试');
        return $this->pass('提交成功,请等待审核');
    }

    public function lists()
    {
        $page     = $this->params['page'] ?? 1;
        $pagesize = $this->params['pagesize'] ?? 20;
        $type = $this->params['type'] ?? 0;
        $pagesize = $pagesize > 20 ? 20 : $pagesize;

        $states = [['in', [-1,0]], 1, 2];
        $data = Db::name('task_universal_records')
                    ->alias('a')
                    ->join('task_universal b', 'a.task_universal_id=b.id')
                    ->master()
                    ->where(['a.user_id'=>$this->member['id'], 'a.state'=>$states[$type],'b.delete_time'=>0,'b.channel'=>1])
                    ->field('b.id,b.name,b.image,a.value,a.state,a.reason,a.images,a.content')
                    ->select();
        foreach ($data as &$value) {
            $value['images'] = $value['images'] ? explode(',', $value['images']) : [];
        }

        return $this->pass($data);
    }

}